package com.com.unionpay.utils;

import com.sdk.LogUtils;

import java.io.File;
import java.io.IOException;

import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * <pre>
 *     author : liuso
 *     e-mail : xxx@xx
 *     time   : 2018/12/03
 *     desc   :
 *     version: 1.0
 * </pre>
 */

public class HttpUtil {
    /**
     * 上传文件
     *
     * @param actionUrl 接口地址
     */
    public static  void upLoadFile(String actionUrl, String speechText, String url) {
        // 创建一个RequestBody，文件的类型是image/png
        OkHttpClient client = new OkHttpClient();
        File file = new File(actionUrl);
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody multipartBody = new MultipartBody.Builder()
                // 设置type为"multipart/form-data"，不然无法上传参数
                .setType(MultipartBody.FORM)
                .addFormDataPart("fileName", "outfile.pcm", requestBody)
                .addFormDataPart("speechText", speechText)
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(multipartBody)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(okhttp3.Call call, IOException e) {
                LogUtils.v("上传失败=" + e.getMessage());

            }

            @Override
            public void onResponse(okhttp3.Call call, Response response) throws IOException {
                LogUtils.v("上传成功"+response.message());
            }
        });
    }
}
